/*
 *所有关于mes_ordermanagement类的业务代码应在此处编写
*可使用repository.调用常用方法，获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*mes_ordermanagementService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
using VOL.Core.BaseProvider;
using VOL.Core.Extensions.AutofacManager;
using VOL.Entity.DomainModels;
using System.Linq;
using VOL.Core.Utilities;
using System.Linq.Expressions;
using VOL.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using LDR.DingManageMent.IRepositories;
using LDR.DingManageMent.IServices;
using OfficeOpenXml.FormulaParsing.Excel.Functions.Math;
using AutoMapper;
using VOL.Production.IRepositories;

namespace LDR.DingManageMent.Services
{
    public partial class mes_ordermanagementService : Imes_ordermanagementService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly Imes_ordermanagementRepository _repository;//访问数据库
        private readonly IMES_ProductionRepository pro_repostory;

        [ActivatorUtilitiesConstructor]
        public mes_ordermanagementService(
            Imes_ordermanagementRepository dbRepository,
            IHttpContextAccessor httpContextAccessor,
            IMES_ProductionRepository pro_repostory)
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            this.pro_repostory = pro_repostory;
            //多租户会用到这init代码，其他情况可以不用
            //base.Init(dbRepository);
        }
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="o_number"></param>
        /// <returns></returns>
        public int UpdShenhe(string o_number)
        {
            var mes = _repository.Find(x => x.o_number == o_number).FirstOrDefault();
            int res = 0;
            if (o_number == "" || o_number == null)
            {
                res = 0;
                return res;
            }
            if (mes.is_xiafa == "已下发")
            {
                res = -1;
                return res;//已下发此状态不允许审核
            }
            if (mes.status == "已审核")
            {
                res = 2;
                return res;
            }
            mes.status = "已审核";
            int count = _repository.Update(mes, true);
            if (count > 0)
            {
                res = 1;
            }
            return res;
        }
        /// <summary>
        /// 反审核
        /// </summary>
        /// <param name="o_number"></param>
        /// <returns></returns>
        public int UpdShenhe2(string o_number)
        {
            var mes = _repository.Find(x => x.o_number == o_number).FirstOrDefault();
            int res = 0;
            if (o_number == "" || o_number == null)
            {
                res = 0;
                return res;
            }
            if (mes.is_xiafa == "已下发")
            {
                res = -1;
                return res;//已下发此状态不允许反审核
            }
            if (mes.status == "未审核")
            {
                res = 2;
                return res;
            }
            mes.status = "未审核";
            int count = _repository.Update(mes, true);
            if (count > 0)
            {
                res = 1;
            }
            return res;
        }
        /// <summary>
        /// 下发生产
        /// </summary>
        /// <param name="o_number"></param>
        /// <returns></returns>
        public int XiaFa(MES_Production data)
        {
            if (data.status == "未审核") {
                return 0;
            }
            MES_Production pro = new MES_Production();
            pro.o_id = "PRO"+DateTime.Now.ToString("yyyyMMddmmssfff");
            pro.o_number = data.o_number;
            pro.status = data.status;
            pro.create_time = data.create_time;
            pro.p_number = "PRO" + DateTime.Now.ToString("yyyyMMddmmssfff");
            pro.is_ruku = data.is_ruku;
            pro.plan_start = DateTime.Now.ToString("yyyy-mm-dd-ss-");
            pro.plan_end = "";
            pro.contract_no = data.contract_no;
            pro.jiaohuo_date = data.jiaohuo_date;
            pro.comment = data.comment;
            pro.old_status = "";
            pro.p_status = "";
            pro.important = 1;
            pro.finish_status = "";
            pro.kh_id = data.kh_id;
            pro.kh_name = data.kh_name;
            pro.is_wuxianzhi = 1;

            pro_repostory.Add(pro, true);
            return 1;
        }
        /// <summary>
        /// 修改金额
        /// </summary>
        /// <param name="all_price"></param>
        /// <returns></returns>
        public int UpdPrice(int all_price)
        {
            var count = 0;
            mes_ordermanagement ordermenagement=new mes_ordermanagement();
            ordermenagement.all_price = all_price;
            var res=_repository.Update(ordermenagement,true);
            if (res > 0) { count = 1; }
            return count;
        }
    }
}
